package cn.s1995.sloth.bms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.AppVersion;
import cn.s1995.sloth.bizs.service.AppVersionService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * app版本信息
 *
 * @author Lex
 * @date 2024-05-11 09:21:45
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/appVersion")
@Tag(description = "AppVersion", name = "APP版本信息管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AppVersionController {

    private final AppVersionService appVersionService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req app版本信息
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_AppVersion_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject AppVersion req) {
        return R.ok(appVersionService.page(page, buildQueryWrapper(req)));
    }

    /**
     * 获取列表
     * @param req app版本信息
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_AppVersion_view')")
    public R getList(@ParameterObject AppVersion req) {
        return R.ok(appVersionService.list(buildQueryWrapper(req)));
    }

    /**
     * 获取对象
     * @param id id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取对象", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_AppVersion_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(appVersionService.getById(id));
    }

    /**
     * 新增app版本信息
     * @param req app版本信息
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增app版本信息", description = "新增app版本信息")
    @SysLog("新增app版本信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_AppVersion_add')")
    public R save(@RequestBody AppVersion req) {
        return R.ok(appVersionService.save(req));
    }

    /**
     * 修改app版本信息
     * @param req app版本信息
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改app版本信息", description = "修改app版本信息")
    @SysLog("修改app版本信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_AppVersion_edit')")
    public R updateById(@RequestBody AppVersion req) {
        return R.ok(appVersionService.updateById(req));
    }

    /**
     * 通过id删除app版本信息
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除app版本信息", description = "通过id删除app版本信息")
    @SysLog("通过id删除app版本信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_AppVersion_del')")
    public R remove(@RequestBody Long[] ids) {
        return R.ok(appVersionService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_AppVersion_export')")
    public List<AppVersion> export(AppVersion req, Long[] ids) {
        return appVersionService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), AppVersion::getId, ids));
    }

	/**
     * 构建查询条件
     * @param req
     * @return
     */
    @NotNull
    private static LambdaQueryWrapper<AppVersion> buildQueryWrapper(AppVersion req) {
        LambdaQueryWrapper<AppVersion> wrapper = Wrappers.lambdaQuery(AppVersion.class);

        wrapper.and(
                StrUtil.isNotEmpty(req.getSearch()),
                w -> w.like(AppVersion::getPlatform, req.getSearch())
                        .or().like(AppVersion::getAppId, req.getSearch())
                        .or().like(AppVersion::getEditionName, req.getSearch())
                        .or().like(AppVersion::getEditionNumber, req.getSearch())
                        .or().like(AppVersion::getRemark, req.getSearch())
        );

		wrapper.eq(StrUtil.isNotBlank(req.getPlatform()), AppVersion::getPlatform, req.getPlatform());
		wrapper.eq(Objects.nonNull(req.getEditionForce()), AppVersion::getEditionForce, req.getEditionForce());
		wrapper.eq(Objects.nonNull(req.getPackageType()), AppVersion::getPackageType, req.getPackageType());
		wrapper.eq(Objects.nonNull(req.getEditionIssue()), AppVersion::getEditionIssue, req.getEditionIssue());
		wrapper.eq(Objects.nonNull(req.getEditionSilence()), AppVersion::getEditionSilence, req.getEditionSilence());
        return wrapper;
    }

}
